<template>
  <div>
    <el-dialog
      v-model="dialogVisible"
      :title="title"
      class="custom-dialog"
      width="500px"
      @close="handleClose"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        class="data-form"
        label-width="100px"
      >
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="新密码" prop="loginPassword">
              <el-input
                v-model="form.loginPassword"
                autocomplete="new-password"
                class="data-input"
                placeholder="请输入"
                show-password
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="确认密码" prop="reLoginPassword">
              <el-input
                v-model="form.reLoginPassword"
                class="data-input"
                placeholder="请输入"
                show-password
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button :loading="loading" @click="handleClose(false)"
          >取消</el-button
        >
        <el-button :loading="loading" type="primary" @click="handleSubmit"
          >确定</el-button
        >
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { resetPwd } from '@/api/manager/systemManage/user.js'

defineOptions({
  name: 'resetPasswordDialog',
})
const emit = defineEmits(['close'])
const title = ref('修改密码')
const dialogVisible = ref(false)
const handleClose = (isFlash) => {
  form.value = {
    loginPassword: '',
    reLoginPassword: '',
    pkid: '',
  }
  formRef.value.clearValidate()
  dialogVisible.value = false
  emit('close', isFlash)
}
const formRef = ref(null)
const form = ref({ loginPassword: '', reLoginPassword: '', pkid: '' })

const rules = ref({
  loginPassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern:
        /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[a-zA-Z0-9\\W_]{8,}$/,
      message: '至少包含大写字母、小写字母、数字和特殊符号中其中三种',
      trigger: 'blur',
    },
  ],
  reLoginPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    {
      pattern:
        /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[a-zA-Z0-9\\W_]{8,}$/,
      message: '至少包含大写字母、小写字母、数字和特殊符号中其中三种',
      trigger: 'blur',
    },
  ],
})

const loading = ref(false)
const handleSubmit = async () => {
  console.log(form.value)
  if (form.value.loginPassword !== form.value.reLoginPassword) {
    ElMessage.warning('确认密码与密码不同，请重新输入')
    return
  }
  if (!formRef.value) return
  await formRef.value.validate(async (valid, fields) => {
    if (valid) {
      loading.value = true
      const params = JSON.parse(JSON.stringify(form.value))
      params.loginPassword = window.btoa(form.value.loginPassword)
      delete params.reLoginPassword

      try {
        let res = await resetPwd(params)
        if (res.success) {
          ElMessage.success('保存成功')
          handleClose(true)
        } else {
          ElMessage.error(res.message)
        }
        loading.value = false
      } catch (error) {
        console.log('error', error)
        loading.value = false
      }
    } else {
      ElMessage.warning('请正确录入信息')
    }
  })
}

const setFormPkid = (pkid) => {
  form.value.pkid = pkid
}

defineExpose({ dialogVisible, title, setFormPkid })
</script>
<style lang="scss" scoped></style>
